public class Practice1 {
    //https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=13&tqId=11192&ru=/exam/oj
    //太久没碰二叉树, 没做出来
    //用返回值用int表示高度这题挺巧妙的, 也可以使用一个类来存储高度和布尔值
    public class Solution {
        /**
         * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
         *
         *
         * @param pRoot TreeNode类
         * @return bool布尔型
         */
        public boolean IsBalanced_Solution (TreeNode pRoot) {
            // write code here
            return dfs(pRoot) != -1;


        }

        private int dfs(TreeNode root){
            if(root == null){
                return 0;
            }
            int left = dfs(root.left);
            if(left == -1){
                return -1;
            }
            int right = dfs(root.right);
            if(right == -1){
                return -1;
            }

            return Math.abs(right - left) <= 1 ? Math.max(right,left) + 1 : -1;

        }


    }
